<?php

class wareController extends baseController
{
    /** 检索材料信息/输入材料关键字进行搜索 */	
    public function search() {
        $minkey     =   2;      //最小输入字符数
        $value      =   fetch_a_string('v');

        if(empty($value) || $minkey>strlen($value)){
            Show::warn(sprintf('输入的检索文字不能少于%d个.', $minkey));
		}

		$slist = WareHelper::getList(WareHelper::makupFuzzyKey($value),false);

#		echo 'Count: ', WareHelper::count(WareHelper::makupFuzzyKey($value)), '<br/>';

		Show::ajaxBand(array('wares'=>$slist));
		Show::note('ok');
    }

	/**
	 * 材料列表
	 */
    public function show()
    {
        $where  =   "1";
        if(fetch_a_string('title')) {
            $where  .=  ' AND '.WareHelper::makupFuzzyKey(fetch_a_string('title'));
        }

        $slist  = WareHelper::getList($where, true, 'sno ASC');
		#Debug::pre($slist);
        $this->assign('wares',     $slist);
        $this->display();
    }

	/** 选择材料的html */
	public function choose()
	{
		$id	= fetch_a_int('id') ?fetch_a_int('id'): 0;
		$stuff	= Ware::getbyid($id);
		if(!$stuff) {
			die("没有找到材料信息"); //Stuff not found!
		}
		$this->assign('ware', $stuff);
#		echo $this->fetch('choose');
		$this->display();
	}

    public function modify()
    {
		if(!fetch_a_int('id')){
			Show::warn('参数出错');
		}

		$ware = Ware::getbyid(fetch_a_int('id'));
		if(!$ware){
			Show::warn("没有找到材料信息");
		}

        if(is_post())
		{
			if(!fetch_a_string('name')){
				Show::warn('材料名称不能为空');
			}
			if(!fetch_a_int('warehouse_id')){
				Show::warn('请选择仓库');
			}

			$data	= array(
                'sno'		=>  fetch_a_string('sno'),
                '_sno'		=>  fetch_a_string('sno'),
                'unit'		=>  fetch_a_string('unit'),
                'brand'		=>  fetch_a_string('brand'),
                'specific'	=>  fetch_a_string('specific'),
                'name'	=>  fetch_a_string('name'),
				'warehouse_id' => fetch_a_int('warehouse_id'),
            );

			//解决分类路径问题
			$c_cls	= count($_POST['class_id'])-1;
			$class_id	= 0;
			for($i=$c_cls;$i>=0;$i--)
			{
				if($class_id = intval($_POST['class_id'][$i])) break;
			}

			if($class_id) {
				$cls = Db::get('WareClass')->select()->where(array('id'=>$class_id))->getOne();
				$data['class_id']	= $class_id;
				#$data['cls_path']	= $cls['path'].$cls['id'].'/';
				$data['sno']		= $cls['code'].'-'.fetch_a_string('sno');
			}
			else {
				$data['class_id']	= '0';
				#$data['cls_path']	= '/';
			}

			$_serial = addslashes($data['sno']);
			if(Db::get('Ware')->count()->where(
					array('sno'=>$_serial),
					array('id','!=',fetch_a_int('id'))
				)->getCol() >0) {
				Show::warn('材料编号已存在');
			}

            if(Db::get('Ware')->update($data)->where(array('id'=>fetch_a_int('id')))->exec()){
				Show::note('操作成功');
            }
            else{
				Show::warn('操作失败');
            }
        }
//Debug::pre($ware);
		$ware['cls_code']	= substr($ware['sno'], 0, strlen($ware['sno'])-strlen($ware['_sno']));
        $this->assign('ware',  $ware);
		$this->assign('warehouse', Warehouse::getList());
        $this->display('ware-add');
    }

    public function add()
    {
        if(is_post())
		{
			if(!fetch_a_string('name')){
				Show::warn('材料名称不能为空');
			}

			$data	= array(
                'sno'		=>  fetch_a_string('sno'),
                '_sno'		=>  fetch_a_string('sno'),
                'unit'		=>  fetch_a_string('unit'),
                'brand'		=>  fetch_a_string('brand'),
                'specific'	=>  fetch_a_string('specific'),
                'name'	=>  fetch_a_string('name'),
				'warehouse_id' => fetch_a_int('warehouse_id'),
            );

			//解决分类路径问题
			$c_cls	= count($_POST['class_id'])-1;
			$class_id	= 0;
			for($i=$c_cls;$i>=0;$i--)
			{
				if($class_id = intval($_POST['class_id'][$i])) break;
			}

			if($class_id) {
				$cls = Db::get('WareClass')->select()->where(array('id'=>$class_id))->getOne();
				$data['class_id']	= $class_id;
				#$data['cls_path']	= $cls['path'].$cls['id'].'/';
				$data['sno']		= $cls['code'].'-'.fetch_a_string('sno');
			}
			else {
				$data['class_id']	= '0';
				#$data['cls_path']	= '/';
			}

			$_serial = addslashes($data['sno']);
			if(Db::get('Ware')->count()->where(array('sno'=>$_serial))->getCol() >0) {
				Show::warn('材料编号已存在');
			}

            if(Db::get('Ware')->insert($data)->exec()){
				Show::note('操作成功');
            }
            else{
				Show::warn('操作失败');
            }
        }

		$this->assign('warehouse', Warehouse::getList());
        $this->display('ware-add');
    }

}

?>
